﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using System.Windows.Controls;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using GoldDigger.AppServices;

namespace GoldDigger.ViewModels
{
    class MainViewModel : ViewModelBase
    {
        #region Declarations

        private ViewModelBase _viewModelToShow;
        private MainService _app;

        private ICommand _returnToHomeScreenCommand;
        private ICommand _closeApplicationCommand;
        private ICommand _dataSummaryCommand;
        private ICommand _featureSelectionCommand;
        private ICommand _associationRulesCommand;
        private ICommand _exportResultsCommad;

        private bool _canGoToSummary;
        private bool _canExportResults;
        private bool _isBusy;

        #endregion

        public MainViewModel()
        {
            _app = new MainService();
            ViewModelToShow = new StartViewModel(_app);
            RegisterChangeVMMessage();
            RegisterBusyMessage();
            RegisterMenuChange();
        }

        #region Observable Properties

        public ViewModelBase ViewModelToShow
        {
            get
            {
                return _viewModelToShow;
            }
            set
            {
                _viewModelToShow = value;
                RaisePropertyChanged("ViewModelToShow");
            }
        }

        public bool IsBusy
        {
            get
            {
                return _isBusy;
            }
            set
            {
                    _isBusy = value;
                    RaisePropertyChanged("IsBusy");
            }
        }

        public ICommand ReturnToHomeScreenCommand
        {
            get
            {
                return _returnToHomeScreenCommand ?? (_returnToHomeScreenCommand = new RelayCommand(SwitchToHomeView));
            }
        }

        public ICommand CloseApplicationCommand
        {
            get
            {
                return _closeApplicationCommand ?? (_closeApplicationCommand = new RelayCommand(Close));
            }
        }

        public ICommand DataSummaryCommand
        {
            get
            {
                return _dataSummaryCommand ?? (_dataSummaryCommand = new RelayCommand(GoToDataSummary));
            }
        }

        public ICommand FeatureSelectionCommand
        {
            get
            {
                return _featureSelectionCommand ?? (_featureSelectionCommand = new RelayCommand(GoToFeatureSelection));
            }
        }
       
        public ICommand AssociationRulesCommand
        {
            get
            {
                return _associationRulesCommand ?? (_associationRulesCommand = new RelayCommand(GoToAssociationRules));
            }
        }

        public ICommand ExportResultsCommand
        {
            get
            {
                return _exportResultsCommad ?? (_exportResultsCommad = new RelayCommand(ExportResults));
            }
        }

        public bool CanGoToSummary
        {
            get
            {
                if(_app.AppData.SourceDT.Columns.Count>0)
                    return true;
                else
                    return false;
            }
            set
            {
                _canGoToSummary = value;
                RaisePropertyChanged("CanGoToSummary");
            }
        }

        public bool CanExportResults
        {
            get
            {
                if (_app.AppData.AprioriData.AssociationRules.Count>0)
                    return true;
                else
                    return false;
            }
            set
            {
                _canExportResults = value;
                RaisePropertyChanged("CanExportResults");
            }
        }

        #endregion

        private void RegisterChangeVMMessage()
        {
            Messenger.Default.Register<NotificationMessage<ViewModelBase>>(this,
                                m =>
                                {
                                    if (m.Notification == "ChangeVM")
                                    {
                                        ViewModelToShow = m.Content;
                                    }
                                });
        }

        private void RegisterBusyMessage()
        {
            Messenger.Default.Register<NotificationMessage<bool>>(
                this,
                m =>
                {
                    if (m.Notification == "Wait")
                    {
                        IsBusy = m.Content;
                    }
                });
        }

        private void RegisterMenuChange()
        {
            Messenger.Default.Register<NotificationMessage<string>>(this,
                                m =>
                                {
                                    if (m.Notification == "UpdateMenu")
                                    {
                                        if(m.Content =="summary")
                                         RaisePropertyChanged("CanGoToSummary");
                                        else if(m.Content == "export")
                                         RaisePropertyChanged("CanExportResults");
                                    }
                                });
        }

        private void SwitchToHomeView()
        {
            _app = new MainService();
            CanGoToSummary = false;
            CanExportResults = false;
            Messenger.Default.Send(new NotificationMessage<ViewModelBase>(new StartViewModel(_app), "ChangeVM"));
        }

        private void Close()
        {
           App.Current.Shutdown();
        }

        private void GoToDataSummary()
        {
            Messenger.Default.Send(new NotificationMessage<ViewModelBase>(new DataSummaryViewModel(_app), "ChangeVM"));
        }

        private void GoToFeatureSelection()
        {
            Messenger.Default.Send(new NotificationMessage<ViewModelBase>(new FeatureSelectionViewModel(_app), "ChangeVM"));
        }

        private void GoToAssociationRules()
        {
            Messenger.Default.Send(new NotificationMessage<ViewModelBase>(new AssociationRuleViewModel(_app), "ChangeVM"));
        }

        private void ExportResults()
        {
            
        }
    }
}
